package org.example;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.*;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {

    public static void test1() throws InterruptedException {
        //启动chrome浏览器
        WebDriver webDriver = new ChromeDriver();

        //打开百度
        webDriver.get("https://www.baidu.com");

        //定位到输入框
        WebElement input = webDriver.findElement(By.name("wd"));
        input.sendKeys("自动化测试");

        //定位搜索按钮
        WebElement button = webDriver.findElement(By.id("su"));
        button.click();

        //sleep(2000);

        //隐式等待
        //webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        WebDriverWait wait = new WebDriverWait(webDriver, 30);
        //判断元素是否可以点击
        //wait.until(ExpectedConditions.elementToBeClickable(By.tagName("em")));

        //判断元素值是否等于"自动化测试"
        wait.until(ExpectedConditions.textToBe(By.tagName("em"), "自动化测试"));

        //检查搜索结果
        List<WebElement> elements = webDriver.findElements(By.tagName("em"));
        for(WebElement element : elements) {
            if(element.getText().equals("自动化测试")) {
                System.out.println("测试通过");
            }else{
                System.out.println("测试失败");
            }
        }
    }

    public static void test2() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        WebElement input = webDriver.findElement(By.id("kw"));
        input.sendKeys("软件测试");
        //<input type="submit" id="su" value="百度一下" class="bg s_btn">
        //获取html属性
        WebElement element = webDriver.findElement(By.id("su"));
        String v = element.getAttribute("value");
        System.out.println(v);

        //关闭浏览器
        webDriver.quit();
    }

    public static void test3() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //获取页面标题和url
        System.out.println("搜索前: ");
        System.out.println("title: " + webDriver.getTitle());
        System.out.println("url: " + webDriver.getCurrentUrl());

        WebElement input = webDriver.findElement(By.id("kw"));
        input.sendKeys("软件测试");
        webDriver.findElement(By.id("su")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        System.out.println("搜索后: ");
        System.out.println("title: " + webDriver.getTitle());
        System.out.println("url: " + webDriver.getCurrentUrl());
    }

    public static void test4() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //浏览器最大化
        webDriver.manage().window().maximize();
        sleep(2000);
        //设置浏览器窗口大小
        webDriver.manage().window().setSize(new Dimension(500, 500));
    }

    public static void test5() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.id("kw")).sendKeys("软件测试");
        webDriver.findElement(By.id("su")).click();
        sleep(2000);
        //返回
        webDriver.navigate().back();
        sleep(2000);
        //前进
        webDriver.navigate().forward();
        sleep(2000);
        //去指定页面
        webDriver.navigate().to("https://www.baidu.com");
    }

    public static void test6() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.id("kw")).sendKeys("软件测试");
        webDriver.findElement(By.id("su")).click();
        sleep(2000);
        //设置滚动条到最底部
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        sleep(2000);
        //设置滚动条到最顶端
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=0");
    }

    public static void test7() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //点击ABC
        webDriver.findElement(By.id("kw")).sendKeys("ABC");
        //点击回车
        webDriver.findElement(By.id("kw")).sendKeys(Keys.ENTER);
        //点击 ctrl + A
        webDriver.findElement(By.id("kw")).sendKeys(Keys.CONTROL, "A");
        //点击 ctrl + X
        webDriver.findElement(By.id("kw")).sendKeys(Keys.CONTROL, "X");
    }

    public static void test8() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        WebElement element = webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)"));
        //把鼠标移动到这个位置上右击,使用perform方法使点击效果能被看到
        Actions action = new Actions(webDriver);
        action.moveToElement(element).contextClick().perform();
    }

    public static void test9() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //点击新闻
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();

        //获取当前浏览器中的所有页面的窗口句柄
        Set<String> handles = webDriver.getWindowHandles();
        String target = "";
        for(String s : handles) {
            target = s;
        }
        //把窗口句柄切换为最后一个窗口
        webDriver.switchTo().window(target);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("特朗普");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
    }

    public static void test10() throws IOException, InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.id("kw")).sendKeys("软件测试");
        webDriver.findElement(By.id("su")).click();

        sleep(3000);
        //截图
        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        //保存
        FileUtils.copyFile(file, new File("C://截图/软件测试.png"));
    }
    public static void test11() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("C:\\Users\\22759\\Desktop\\spring\\AutoTest20240719\\src\\main\\resources\\page\\inner.html");

        //切换到frame
        webDriver.switchTo().frame("f1");
        sleep(2000);
        //点击click
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

    public static void test12() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("C:\\Users\\22759\\Desktop\\spring\\AutoTest20240719\\src\\main\\resources\\page\\test04.html");

        //定位到select标签元素
        WebElement element = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        //创建Select对象
        Select select = new Select(element);
        //通过序号选择，序号从0开始
        select.selectByIndex(2);
        sleep(3000);
        //通过选项value值选择
        select.selectByValue("9.25");
    }

    public static void test13() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("C:\\Users\\22759\\Desktop\\spring\\AutoTest20240719\\src\\main\\resources\\page\\test0502.html");
        webDriver.findElement(By.cssSelector("input")).click();

        //切换到弹窗,输入内容
        webDriver.switchTo().alert().sendKeys("Ting");
        sleep(2000);
        //点击确定
        webDriver.switchTo().alert().accept();

        //点击取消
        //webDriver.switchTo().alert().dismiss();
    }

    public static void test14() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("C:\\Users\\22759\\Desktop\\spring\\AutoTest20240719\\src\\main\\resources\\page\\test07.html");
        webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

        //上传文件
        webDriver.findElement(By.cssSelector("body > div > div > input[type=file]")).sendKeys("C:\\Users\\22759\\Desktop\\spring\\AutoTest20240719\\src\\main\\resources\\page\\test07.html");
    }

    public static void main(String[] args) throws InterruptedException, IOException {
        test14();
    }
}